<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>

<html>
<head>
    <title>登录页</title>
    <%--echart线图、柱状图、饼图等各种图型库 cdn--%>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
    <%--jquery cdn--%>
    <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
</head>
<body>
状态 <input name="status"><br>
<button onclick="zzt()">点击展示柱状图</button>
<button onclick="excel()">导出Excel</button>

<div id="main" style="width: 500px;height: 500px"></div>

</body>

<script>



    function excel() {
        let status = $('[name=status]').val();
        location.href = '/excel.do?status=' + status
    }


    function zzt() {

        let status = $('[name=status]').val();

        $.ajax({
            url: '/zzt.do?status=' + status,
            success: function (data) {

                console.log(data)


                var x = []
                var y = []

                //找到data中的Y轴最大值
                var maxY = 0;

                for (let i = 0; i < data.length; i++) {
                    if (data[i].ydata > maxY) {
                        maxY = data[i].ydata
                    }
                }

                for (let i = 0; i < data.length; i++) {
                    x.push(data[i].xdata)
                    let ydata = data[i].ydata;

                    if (ydata == maxY){ //如果当前的y轴是最大值，则设置为红柱子
                        y.push({
                            value: ydata,
                            itemStyle: {
                                color: '#a90000'
                            }
                        })
                    }else {
                        y.push(ydata)
                    }


                }
                var chartDom = document.getElementById('main');
                var myChart = echarts.init(chartDom);
                var option;

                option = {
                    xAxis: {
                        type: 'category',
                        data: x
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            data: y,
                            type: 'bar',
                            showBackground: true,
                            backgroundStyle: {
                                color: 'rgba(180, 180, 180, 0.2)'
                            }
                        }
                    ]
                };

                option && myChart.setOption(option);

            }
        })
    }
</script>
</html>